// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// S [author] rhyme_lph
// t [email]  rhymelph@gmail.com
// D [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/entities/setting_data_entity.dart';
import 'package:finance_app/src/provider/dashboard/amount_gst/amount_gst_rate_category_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:finance_app/src/utils/input_formatter_tools.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:finance_app/src/widgets/button/normal_text_button.dart';
import 'package:finance_app/src/widgets/common_app_bar.dart';
import 'package:finance_app/src/widgets/form/gst_inclusive_form.dart';
import 'package:finance_app/src/widgets/form/value_select_form.dart';
import 'package:finance_app/src/widgets/input/form_input_field.dart';
import 'package:finance_app/src/widgets/wrapper/normal_form_wrapper.dart';
import 'package:flutter/material.dart';
import 'package:tuple/tuple.dart';

export 'package:finance_app/src/entities/setting_data_entity.dart';

@RouterMeta(paramName: "amountGstRateCategoryPage")
class AmountGstRateCategoryPage extends StatefulWidget {
  final SettingDataCategory category;
  final SettingDataCurrency unit;
  final SettingDataTax tax;
  final bool isCheckInclusive;
  final String amount;
  final String type;
  const AmountGstRateCategoryPage(
      {Key key,
      this.category,
      this.tax,
      this.unit,
      this.isCheckInclusive,
      this.amount,
      this.type})
      : super(key: key);

  @override
  _AmountGstRateCategoryPageState createState() =>
      _AmountGstRateCategoryPageState();
}

class _AmountGstRateCategoryPageState extends State<AmountGstRateCategoryPage>
    with
        PagePresenter<AmountGstRateCategoryPage,
            AmountGstRateCategoryProvider> {
  @override
  void initState() {
    provider.type = widget.type;
    provider.gstRate = Config.settingInfo.taxData;
    super.initState();
    provider.initData(
        widget.category, widget.tax, widget.isCheckInclusive, widget.amount);
  }

  @override
  PreferredSizeWidget get appBar => getDefaultAppBar(
          title: Config.currentCompany.enableGst != 1
              ? S.current.amount
              : S.current.amount__gst_rate,
          actions: [
            if (provider.isShowDelete) buildDeleteBtn(),
          ]);

  Widget buildDeleteBtn() {
    return NormalDeleteTextButton(
      onTap: provider.onDelete,
    );
  }

  @override
  Widget get buildWidget => ListView(
        children: [
          listBuilder<String>(
            selector: (_, __) => __.category?.name,
            builder: (BuildContext context, value, Widget child) =>
                ValueSelectForm(
              onTap: provider.onSelectCategory,
              title: S.current.category,
              value: value,
            ),
          ),
          FromInputField(
            tip: S.current.amount,
            controller: provider.amountController,
            focusNode: provider.amountFocusNode,
            inputFormatterList: [
              doubleInputAllowNegativeFormatter,
            ],
            //fixed for samsung keyboard can't enter negative amount
            // keyboardType:
            //     TextInputType.numberWithOptions(decimal: true, signed: true),
          ),
          Visibility(
            visible: Config.currentCompany.enableGst == 1,
            child: listBuilder<String>(
              selector: (_, __) => __.gstRate?.displayName,
              builder: (BuildContext context, String value, Widget child) =>
                  Column(
                children: [
                  ValueSelectForm(
                    onTap: provider.onSelectGstRate,
                    title: S.current.gst_rate,
                    value: value,
                  ),
                  listBuilder<bool>(
                    selector: (_, __) => __.enable,
                    builder: (BuildContext context, bool value, Widget child) =>
                        value == true
                            ? listBuilder<Tuple3<bool, String, String>>(
                                selector: (_, __) => Tuple3(__.isCheckInclusive,
                                    __.getGSTInfo(), __.getTotal()),
                                builder: (BuildContext context,
                                        Tuple3<bool, String, String> value,
                                        Widget child) =>
                                    NormalFormWrapper(
                                  child: GstInclusiveForm(
                                    isCheckInclusive: value.item1,
                                    unit: widget.unit,
                                    gstInfo: value.item2,
                                    total: value.item3,
                                    onChangeInclusive:
                                        provider.onChangeInclusive,
                                  ),
                                ),
                              )
                            : Container(),
                  ),
                ],
              ),
            ),
          ),
          AppSpaces.h30,
          listBuilder<bool>(
            selector: (_, __) => __.enable,
            builder: (BuildContext context, bool value, Widget child) =>
                NormalButton(
              title: S.current.done,
              onTap: value == true ? provider.onNavDone : null,
            ),
          ),
        ],
      );
}
